package com.efast.dmom.mgmt.service.mes.impl;
import java.util.ArrayList;
import java.util.List;

import com.efast.dmom.mgmt.dao.mes.TrBasProductCfgBiDao;
import com.efast.dmom.mgmt.dao.mes.TwBasLotConsumpationDao;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgBi;
import com.efast.dmom.mgmt.entity.mes.TwBasLotConsumpation;
import com.efast.dmom.mgmt.service.mes.TrBasProductCfgBiService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TrBasProductCfgBiServiceImpl implements TrBasProductCfgBiService{
    @Autowired
    private TrBasProductCfgBiDao trBasProductCfgBiDao;
    @Autowired
    private TwBasLotConsumpationDao twBasLotConsumpationDao;
    
    @Override
    public long getTrBasProductCfgBiRowCount(Assist assist){
        return trBasProductCfgBiDao.getTrBasProductCfgBiRowCount(assist);
    }
    @Override
    public List<TrBasProductCfgBi> selectTrBasProductCfgBi(Assist assist){
        return trBasProductCfgBiDao.selectTrBasProductCfgBi(assist);
    }
    @Override
    public TrBasProductCfgBi selectTrBasProductCfgBiByObj(TrBasProductCfgBi obj){
        return trBasProductCfgBiDao.selectTrBasProductCfgBiByObj(obj);
    }
    @Override
    public TrBasProductCfgBi selectTrBasProductCfgBiById(Integer id){
        return trBasProductCfgBiDao.selectTrBasProductCfgBiById(id);
    }
    @Override
    public int insertTrBasProductCfgBi(TrBasProductCfgBi value){
        return trBasProductCfgBiDao.insertTrBasProductCfgBi(value);
    }
    @Override
    public int insertNonEmptyTrBasProductCfgBi(TrBasProductCfgBi value){
        return trBasProductCfgBiDao.insertNonEmptyTrBasProductCfgBi(value);
    }
    @Override
    public int insertTrBasProductCfgBiByBatch(List<TrBasProductCfgBi> value){
        return trBasProductCfgBiDao.insertTrBasProductCfgBiByBatch(value);
    }
    @Override
    public int deleteTrBasProductCfgBiById(Integer id){
        return trBasProductCfgBiDao.deleteTrBasProductCfgBiById(id);
    }
    @Override
    public int deleteTrBasProductCfgBi(Assist assist){
        return trBasProductCfgBiDao.deleteTrBasProductCfgBi(assist);
    }
    @Override
    public int updateTrBasProductCfgBiById(TrBasProductCfgBi enti){
        return trBasProductCfgBiDao.updateTrBasProductCfgBiById(enti);
    }
    @Override
    public int updateTrBasProductCfgBi(TrBasProductCfgBi value, Assist assist){
        return trBasProductCfgBiDao.updateTrBasProductCfgBi(value,assist);
    }
    @Override
    public int updateNonEmptyTrBasProductCfgBiById(TrBasProductCfgBi enti){
        return trBasProductCfgBiDao.updateNonEmptyTrBasProductCfgBiById(enti);
    }
    @Override
    public int updateNonEmptyTrBasProductCfgBi(TrBasProductCfgBi value, Assist assist){
        return trBasProductCfgBiDao.updateNonEmptyTrBasProductCfgBi(value,assist);
    }

    public TrBasProductCfgBiDao getTrBasProductCfgBiDao() {
        return this.trBasProductCfgBiDao;
    }

    public void setTrBasProductCfgBiDao(TrBasProductCfgBiDao trBasProductCfgBiDao) {
        this.trBasProductCfgBiDao = trBasProductCfgBiDao;
    }
    
	@Override
	public List<TrBasProductCfgBi> getCfgConsumpation(Integer productConfigId) {
		Assist cfgAssist = new Assist();
		cfgAssist.setRequires(Assist.andEq("product_config_id", productConfigId));
		List<TrBasProductCfgBi> cfgBiList = trBasProductCfgBiDao.selectTrBasProductCfgBi(cfgAssist);
		return cfgBiList;
	}
	
	@Override
	public int updateTrBasProductCfgBiByBatch(List<TrBasProductCfgBi> value) {
		return trBasProductCfgBiDao.updateTrBasProductCfgBiByBatch(value);
	}
	
	@Override
	public List<TrBasProductCfgBi> selectTrBasProductCfgBiAndPn(Integer lotId,Integer productConfigId,Integer operationId) {
		//通过lotId和当前工序下判断产品执行中是否有物料消耗记录
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("lot_id", lotId));
		assist.setRequires(Assist.andEq("operation_id", operationId));
		List<TwBasLotConsumpation> lotConsumpation = twBasLotConsumpationDao.selectTwBasLotConsumpation(assist);
		List<TrBasProductCfgBi> bi = new ArrayList<TrBasProductCfgBi>();
		if(lotConsumpation!=null&&lotConsumpation.size()>0) {
			bi = trBasProductCfgBiDao.selectTrBasProductCfgBiAndPn(lotId,operationId);
		}else {
			bi = trBasProductCfgBiDao.getCfgConfig(productConfigId);
		}
		return bi;
	}

}